Shader "XC/Unlit_ShadowOnlyPlan"
{
    Properties
    {
        _ShadowIntensity ("Shadow Intensity", Range(0, 1)) = 0.8
    }
    SubShader
    {
        Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" }
        
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off

        Pass
        {
            Name "Unlit"
            Tags
            {
                "LightMode" = "UniversalForward"
            }

            HLSLPROGRAM
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            // 引入光照相关代码，阴影相关代码位于该引用中
		    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            CBUFFER_START(UnityPerMaterial)
            float _ShadowIntensity;
            CBUFFER_END
            
            #pragma vertex UnlitPassVertex
            #pragma fragment UnlitPassFragment
            // 加入阴影对应的关键字
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE

            struct Attributes
            {
                float4 positionOS : POSITION;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float3 worldPos : TEXCOORD1; // 阴影计算需要世界坐标，由顶点着色器传入
            };

            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);

            Varyings UnlitPassVertex(Attributes input)
            {
                Varyings output;

                VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS.xyz);
                output.positionCS = positionInputs.positionCS;
                output.worldPos = TransformObjectToWorld(input.positionOS.xyz); // 阴影计算需要世界坐标，由顶点着色器传入
                return output;
            }

            float4 UnlitPassFragment(Varyings input) : SV_Target
            {
                // 计算出对应的阴影空间
                float4 shadowCoord = TransformWorldToShadowCoord(input.worldPos);
                // 获取主光源的阴影信息
                Light mainLight = GetMainLight(shadowCoord);
                half shadow = mainLight.shadowAttenuation;
                // 获取实时光源的阴影信息
                // half shadow = MainLightRealtimeShadow(shadowCoord);
                
                // return float4(0, 0, 0, 1 - shadow);
                
                float4 shadowColor = lerp(0, float4(0, 0, 0, _ShadowIntensity), 1 - shadow);
                return shadowColor;
            }
            ENDHLSL
        }
    }
}
